package com.abhimanyu.dataaccess;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.abhimanyu.dataaccess.model.DollarRate;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

/**
 * @author Abhimanyu
 * 
 */
public class GoogleDatastoreDollarRateDao extends
		AbstractGoogleDatastoreDao<DollarRate> {

	private String dollarRateColumnName;
	private String dateColumnName;
	private String serviceNameColumnName;

	public void save(DollarRate dollarRate) {

		Entity dollarRateEntity = new Entity(getTableName());

		dollarRateEntity.setProperty(getDollarRateColumnName(),
				dollarRate.getDollarRate());
		dollarRateEntity.setProperty(getDateColumnName(), dollarRate.getDate());
		dollarRateEntity.setProperty(getServiceNameColumnName(),
				dollarRate.getServiceName());

		getDatastoreService().put(dollarRateEntity);
	}

	@Override
	public List<DollarRate> fetchAll() {
		List<DollarRate> listOfDollarRates = null;
		DollarRate rate = null;
		Query query = new Query(getTableName());
		PreparedQuery preparedQuery = getDatastoreService().prepare(query);

		for (Entity dollarRateEntity : preparedQuery.asIterable()) {
			if (listOfDollarRates == null) {
				listOfDollarRates = new ArrayList<DollarRate>();
			}

			rate = constructDollarRate(dollarRateEntity);
			listOfDollarRates.add(rate);
		}
		return listOfDollarRates;
	}

	DateFormat format = new SimpleDateFormat();

	private DollarRate constructDollarRate(Entity dollarRateEntity) {
		DollarRate dollarRate = new DollarRate();
		if (dollarRateEntity.hasProperty(getDateColumnName())) {
			dollarRate.setDate((Date) dollarRateEntity
					.getProperty(getDateColumnName()));
		}
		if (dollarRateEntity.hasProperty(getDollarRateColumnName()))
			dollarRate.setDollarRate(((String) dollarRateEntity
					.getProperty(getDollarRateColumnName())));

		if (dollarRateEntity.hasProperty(getServiceNameColumnName()))
			dollarRate.setServiceName((String) dollarRateEntity
					.getProperty(getServiceNameColumnName()));

		return dollarRate;
	}

	@Override
	public void update(DollarRate object) {
		// TODO Auto-generated method stub

	}

	public String getDollarRateColumnName() {
		return dollarRateColumnName;
	}

	public void setDollarRateColumnName(String dollarRateColumnName) {
		this.dollarRateColumnName = dollarRateColumnName;
	}

	public String getDateColumnName() {
		return dateColumnName;
	}

	public void setDateColumnName(String dateColumnName) {
		this.dateColumnName = dateColumnName;
	}

	public String getServiceNameColumnName() {
		return serviceNameColumnName;
	}

	public void setServiceNameColumnName(String serviceNameColumnName) {
		this.serviceNameColumnName = serviceNameColumnName;
	}

}
